﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using WarehouseManager.Core;
using WarehouseManager.Model;

namespace WarehouseManager.DataAccess
{
    class PhieunhapAccess
    {
        dbConnection dbCon = new dbConnection();
        public PhieunhapAccess() { }
        public DataTable laytoanbophieunhap()
        {
            return dbCon.executeSelectProcedureQueryNonParameter("proc_selectPhieuNhap");
        }
        public DataView timkiemMapn(string MaPN)
        {
            DataView dataView = laytoanbophieunhap().DefaultView;
            dataView.RowFilter = string.Format("maPN like '%{0}%'", MaPN);
            return dataView;
        }
        public void themPhieunhap(Phieunhap pn)
        {
            SqlParameter[] listParams = new SqlParameter[4];
            SqlParameter mapn = new SqlParameter("@maPN", SqlDbType.VarChar, 10);
            mapn.Value = pn.maPN1;
            SqlParameter maNCC = new SqlParameter("@maNCC", SqlDbType.VarChar, 10);
            maNCC.Value = pn.maNCC1;
            SqlParameter maNV = new SqlParameter("@maNV", SqlDbType.VarChar, 10);
            maNV.Value = pn.maNV1;
            listParams[0] = mapn;
            listParams[1] = maNCC;
            listParams[2] = new SqlParameter("@ngayNhap", string.Format("{0:yyyy-MM-dd}",pn.ngayNhap1));
            listParams[3] = maNV;
            dbCon.executeInsertProcedureQuery("proc_insertPhieuNhap",listParams);
        }
        public void suaPhieunhap(Phieunhap pn)
        {
            SqlParameter[] listParams = new SqlParameter[4];
            SqlParameter mapn = new SqlParameter("@maPN", SqlDbType.VarChar, 10);
            mapn.Value = pn.maPN1;
            SqlParameter maNCC = new SqlParameter("@maNCC", SqlDbType.VarChar, 10);
            maNCC.Value = pn.maNCC1;
            SqlParameter maNV = new SqlParameter("@maNV", SqlDbType.VarChar, 10);
            maNV.Value = pn.maNV1;
            listParams[0] = mapn;
            listParams[1] = maNCC;
            listParams[2] = new SqlParameter("@ngayNhap", string.Format("{0:yyyy-MM-dd}", pn.ngayNhap1));
            listParams[3] = maNV;
            dbCon.executeUpdateProcedureQuery("proc_updatePhieuNhap", listParams);
        }
        public void xoaPhieunhap(Phieunhap pn)
        {
            SqlParameter[] listParams = new SqlParameter[1];
            SqlParameter mapn = new SqlParameter("@maPN", SqlDbType.VarChar, 10);
            mapn.Value = pn.maPN1;
            listParams[0] = mapn;
            dbCon.executeDeleteProcedureQuery("proc_deletePhieuNhap", listParams);
        }
        public DataTable layView(string s)
        {
            SqlParameter[] listParams = new SqlParameter[1];
            SqlParameter mapn = new SqlParameter("@maPN", SqlDbType.VarChar, 10);
            mapn.Value = s;
            listParams[0] = mapn;
            return dbCon.executeSelectProcedureQuery("proc_selectIDViewPhieuNhap",listParams);
        }
        public DataTable layHanghoatheoMapn(string s)
        {
            SqlParameter[] listParams = new SqlParameter[1];
            SqlParameter mapn = new SqlParameter("@maPN", SqlDbType.VarChar, 10);
            mapn.Value = s;
            listParams[0] = mapn;
            return dbCon.executeSelectProcedureQuery("tt_listHanghoa_PN", listParams);
        }
    }
}
